package com.wxboot.game.eliminate.config;

import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionConfig;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizePolicy;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HazelCastConfig {

    @Bean
    public Config hazelCastCacheConfig() {
        EvictionConfig evictionConfig = new EvictionConfig();
        evictionConfig.setSize(500)
                .setEvictionPolicy(EvictionPolicy.LRU)
                .setMaxSizePolicy(MaxSizePolicy.FREE_HEAP_SIZE);

        MapConfig mapConfig = new MapConfig();
        mapConfig.setName("configuration")
                .setEvictionConfig(evictionConfig)
                .setTimeToLiveSeconds(-1);

        Config config = new Config();
        config.getNetworkConfig()
                .setPort(10086).setPortCount(100)
                .getJoin().setTcpIpConfig(
                        new TcpIpConfig().setEnabled(true)
                                .addMember("127.0.0.1")
                                .setConnectionTimeoutSeconds(20));

        config.setInstanceName("hazelcast-instance")
                .addMapConfig(mapConfig);
        return config;
    }

    @Bean
    public HazelcastInstance hazelcastInstance(@Autowired Config config) {
        return Hazelcast.newHazelcastInstance(config);
    }

}
